<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SWFObjectPortlet.php 292 2008-11-14 17:44:15Z steen.rabol@gmail.com $
 * @package Pradoportal.Porttlets.ImagePortlet.Edit
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.Data.PortalConfigRecord');

class SWFObjectPortlet extends Portlet
{
	const PORTLET_CONFIG_PREFIX	= "SWFObjectPortlet_";

	private	$_portletconfig = null;

	public function getPortletConfig()
	{
		if($this->_portletconfig == null)
		{
			if($this->ConfigRecord)
				$this->_portletconfig = Prado::unserialize($this->ConfigRecord->value);
		}

		return $this->_portletconfig;
	}

	public function onInit($param)
	{
		parent::onInit($param);

		$name = self::PORTLET_CONFIG_PREFIX . $this->Parameters['id'];
		$this->ConfigRecord	= PortalConfigRecord::finder()->findByname($name);

		if($this->ConfigRecord === null)
		{
			$value			= Prado::serialize(array(
												"swfname"	=> "",
												"width"		=> "",
												"height"	=> "",
												"flashvars"	=> "",
												"quality"	=> "high",
												"wmode"		=> "transparent",
												"cssclass"	=> ""));
			$this->ConfigRecord	= new PortalConfigRecord();
			$this->ConfigRecord->name	= $name;
			$this->ConfigRecord->value	= $value;
			$this->ConfigRecord->save();
		}

		// Set validation for the swfobject
		$this->swfNameVal->ValidationGroup		= "swf_" . $this->Parameters['id'];
		$this->swfQualityVal->ValidationGroup	= "swf_" . $this->Parameters['id'];
		$this->swfWMode->ValidationGroup		= "swf_" . $this->Parameters['id'];
		$this->SaveSWF->ValidationGroup			= "swf_" . $this->Parameters['id'];


		// Set Validation group for Upload
		$this->swfUploadVal->ValidationGroup	= "swfUpload_" . $this->Parameters['id'];
		$this->swfSendFile->ValidationGroup		= "swfUpload_" . $this->Parameters['id'];
	}

	public function dataBind()
	{
		$this->SWFCssClass->Text	= TPropertyValue::ensureString((isset($this->PortletConfig['cssclass']) ? $this->PortletConfig['cssclass'] : ""));
		$this->SWFWidth->Text		= TPropertyValue::ensureString($this->PortletConfig['width']);
		$this->SWFHeight->Text		= TPropertyValue::ensureString($this->PortletConfig['height']);
		$swffilespath				= $this->Application->PortalBasePath . '/storage/media';
		$swffiles					= PortalUtil::getFilesInPath($swffilespath,"name");
		$this->SWFName->DataSource	= $swffiles;
		$this->SWFName->dataBind();

		if(is_array($swffiles))
		{
			foreach($swffiles as $im)
			{
				 if(in_array($this->PortletConfig['swfname'],$im))
					$this->SWFName->SelectedValue = $this->PortletConfig['swfname'];
			}
		}
	}

	public function onLoad($p)
	{
		parent::onLoad($p);
		if(!$this->Page->isPostBack)
		{
			$this->dataBind();
			// Set the MAX file size to the value in php.ini
			$this->FileUpload->MaxFileSize	= PortalUtil::return_bytes(ini_get('upload_max_filesize'));
			$this->ltFileSize->Text			= ini_get('upload_max_filesize');
		}
	}

	public function SaveSWF($sender,$param)
	{
		$swfname	= $this->SWFName->SelectedValue;
		$width		= $this->SWFWidth->Text;
		$height		= $this->SWFHeight->Text;
		$cssclass	= $this->SWFCssClass->Text;
		$quality	= $this->SWFQuality->SelectedValue;
		$wmode		= $this->SWFWmode->SelectedValue;
		$value		= Prado::serialize(array(
											"swfname"	=> $swfname,
											"width"		=> $width,
											"height"	=> $height,
											"quality"	=> $quality,
											"wmode"		=> $wmode,
											"cssclass"	=> $cssclass));

		$this->_portletconfig		= prado::unserialize($value);
		$this->ConfigRecord->value	= $value;
		$this->ConfigRecord->save();
	}

	public function CancelContent($sender,$param)
	{
		$this->dataBind();
	}

	public function DeleteButtonClicked($sender,$param)
	{
		// Call Parent to do system housekeeping
		parent::DeleteButtonClicked($sender,$param);
	}

	public function onFileUpload($sender,$param)
	{
		if($sender->HasFile)
		{
			$savepath	= str_replace("\\", "/",dirname($this->getRequest()->getApplicationFilePath()));
			$savepath 	.= '/';
			$savepath	.= 'storage/media/';
			$savepath	.= $sender->FileName;

			$sender->saveAs($savepath);
			$imagefilespath	= $this->Application->PortalBasePath . '/storage/media';
			$imagefiles = PortalUtil::getFilesInPath($imagefilespath,"name");
			$this->SWFName->DataSource	= $imagefiles;
			$this->SWFName->dataBind();

			if(is_array($imagefiles))
			{
				foreach($imagefiles as $im)
				{
					 if(in_array($sender->FileName,$im))
						$this->SWFName->SelectedValue = $sender->FileName;
				}
			}

			$imgattr	= getimagesize($savepath);
			if(is_array($imgattr))
			{
				$this->SWFWidth->Text	= $imgattr[0];
				$this->SWFHeight->Text	= $imgattr[1];
			}
		}
	}
}
?>